﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;



/// <summary>
/// 弹窗管理
/// </summary>
public class UISystemFacade:Singleton<UISystemFacade>
{
    /// <summary>打开的界面 第一个为类型，第二个为ID</summary>
    Dictionary<int, Dictionary<int, WindowBase>> OpenedWindowDict = new Dictionary<int, Dictionary<int, WindowBase>>();

    /// <summary>等着要打开的界面</summary>
    private List<int> WaitForOpenList = new List<int>();

    public CanvasRoot canvasRoot;

    /// <summary>每个窗口的唯一ID</summary>
    private int instIndex = 0;

    private Dictionary<int, WindowData> WindowDict = new Dictionary<int, WindowData>();

    public void InitSystem()
    {
        GameObject go = ResourceSystem.ResourceSystemFacade.Inst.LoadResource<GameObject>("CanvasRoot");
        if (go != null)
        {
            GameObject instance=UnityEngine.Object.Instantiate(go);
            canvasRoot = instance.GetComponent<CanvasRoot>();
        }
        WindowInfo info= ResourceSystem.ResourceSystemFacade.Inst.LoadResource<WindowInfo>("WindowInfo");
        for (int i = 0; i < info.Data.Count; i++)
        {
            WindowDict.Add(info.Data[i].ID, info.Data[i]);
        }
    }

    /// <summary>
    /// 在第一层的画布打开界面
    /// </summary>
    /// <param name="windowType"></param>
    /// <returns></returns>
    private WindowBase CreateFirstWindow(int windowType)
    {
        return CreateWindow(windowType, canvasRoot.FirstWindowCanvas);
    }

    /// <summary>
    /// 在最顶层的画布打开界面
    /// </summary>
    /// <param name="windowType"></param>
    /// <returns></returns>
    private WindowBase CreateTopWindow(int windowType)
    {
        return CreateWindow(windowType, canvasRoot.TopWindowCanvas);
    }

    /// <summary>
    /// 在第二层的画布打开界面
    /// </summary>
    /// <param name="windowType"></param>
    /// <returns></returns>
    private WindowBase CreateSecondWindow(int windowType)
    {
        return CreateWindow(windowType, canvasRoot.SecondWindowCanvas);
    }

    /// <summary>
    /// 创建界面
    /// </summary>
    /// <param name="windowType"></param>
    /// <param name="canvas"></param>
    /// <returns></returns>
    private WindowBase CreateWindow(int windowType, Canvas canvas)
    {
        WindowData info;
        WindowDict.TryGetValue(windowType, out info);
        if (info != null)
        {

            GameObject go = ResourceSystem.ResourceSystemFacade.Inst.LoadResource<GameObject>(info.PrefabName);

            if (go != null)
            {
                GameObject returnObj = CanvasRoot.Instantiate(go, canvas.transform);

                WindowBase win = returnObj.GetComponent<WindowBase>();
                win.CurWindowInfo = info;
                win.WindowsId = instIndex++;
                return win;
            }
            else
            {
                DebugUtils.LogError("The Target not found");
            }
        }
        return null;
    }

    /// <summary>
    /// 打开界面
    /// </summary>
    /// <param name="windowType"></param>
    /// <returns></returns>
    public GameObject OpenWindow(int windowType)
    {
        WindowData info = null;
        WindowDict.TryGetValue(windowType, out info);
        WindowBase win;
        Dictionary<int, WindowBase> temp;
        if (info != null)
        {
            if (info.IsSingleton)
            {
                if (OpenedWindowDict.TryGetValue(windowType, out temp))
                {
                    if (temp.Count > 0)
                    {
                        temp.First().Value.ShowWidget();
                        return temp.First().Value.gameObject;
                    }
                    else
                    {
                        win = CreateWindowByLayer(info.Layer, windowType);
                        temp.Add(win.WindowsId, win);
                    }
                }
                else
                {
                    win = CreateWindowByLayer(info.Layer, windowType);
                    OpenedWindowDict.Add(windowType, new Dictionary<int, WindowBase>() { { win.WindowsId, win } });
                }
            }
            else
            {
                win = CreateWindowByLayer(info.Layer, windowType);
                if (OpenedWindowDict.TryGetValue(windowType, out temp))
                {
                    temp.Add(win.WindowsId, win);
                }
                else
                {
                    OpenedWindowDict.Add(windowType, new Dictionary<int, WindowBase>() { { win.WindowsId, win } });
                }
            }
            return win.gameObject;
        }
        return null;
    }

    private WindowBase CreateWindowByLayer(int layer, int windowType)
    {
        switch (layer)
        {
            case 0:
                return CreateTopWindow(windowType);
            case 1:
                return CreateFirstWindow(windowType);
            case 2:
                return CreateSecondWindow(windowType);
            default:
                return CreateSecondWindow(windowType);
        }
    }

    /// <summary>
    /// 通过ID获取窗体
    /// </summary>
    /// <param name="windowTypeId">类型ID</param>
    /// <param name="windowId">唯一窗体ID</param>
    /// <returns></returns>
    public WindowBase GetWindowByTypeId(int windowTypeId, int windowId = 0)
    {
        Dictionary<int, WindowBase> tempDict;
        if (OpenedWindowDict.TryGetValue(windowTypeId, out tempDict))
        {
            if (windowId != 0)
            {
                if (tempDict.ContainsKey(windowId))
                {
                    return tempDict[windowId];
                }
            }
            else
            {
                if (tempDict.Count > 0)
                {
                    return tempDict.First().Value;
                }
            }
        }
        return null;
    }

    /// <summary>
    /// 通过界面ID关闭打开的界面
    /// </summary>
    /// <param name="windowId"></param>
    public void CloseOpenWindowById(int windowId)
    {
        foreach (var winDict in OpenedWindowDict)
        {
            foreach (var id in winDict.Value.Keys)
            {
                if (id == windowId)
                {
                    if (winDict.Value[id].CurWindowInfo.Resident)
                    {
                        winDict.Value[id].HideWidget();
                    }
                    else
                    {
                        winDict.Value[id].CloseWindow();
                        winDict.Value.Remove(id);
                    }
                }
            }
        }
    }

    /// <summary>
    /// 通过界面类型关闭打开的界面
    /// </summary>
    /// <param name="windowType"></param>
    public void CloseOpenWindowByType(int windowType)
    {
        Dictionary<int, WindowBase> winDict;
        if (OpenedWindowDict.TryGetValue(windowType, out winDict))
        {
            foreach (var win in winDict)
            {
                if (win.Value.CurWindowInfo.Resident)
                {
                    win.Value.HideWidget();
                }
                else
                {
                    win.Value.CloseWindow();
                }
            }
        }
    }
}